<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>后台管理系统</title>
    <!-- 引入bootstrap样式 -->
    <link th:href="@{/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">
    <!--图标库-->
    <link th:href="@{/lyear/css/materialdesignicons.min.css}" rel="stylesheet" type="text/css"/>
    <!--弹出框样式-->
    <link th:href="@{/lyear/jconfirm/jquery-confirm.min.css}" rel="stylesheet" type="text/css"/>
    <!--自定义样式-->
    <link href="../lyear/css/style.min.css" th:href="@{/lyear/css/style.min.css}" rel="stylesheet" type="text/css"/>

</head>
<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <div th:replace="admin/common/leftbar::#leftbaraside"></div>
        <!--头部信息-->
        <div th:replace="admin/common/topbar::#topbarheader"></div>

        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <h4>用户信息</h4>
                            </div>

                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover table-striped">
                                        <thead>
                                        <tr>
                                            <th class="info">序号</th>
                                            <th class="info">用户名</th>
                                            <th class="info">真实姓名</th>
                                            <th class="info">手机号</th>
                                            <th class="info">邮箱</th>
                                            <th class="info">相关操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr th:each="user:${userList}">
                                            <td th:text="${(userPageInfo.pageNum - 1) * userPageInfo.pageSize+userStat.index+1}"></td>
                                            <td th:text="${user.username}"></td>
                                            <td th:text="${user.realname}==null?'未填写':${user.realname}"></td>
                                            <td th:text="${user.phone}==null?'未填写':${user.phone}"></td>
                                            <td th:text="${user.email}==null?'未填写':${user.email}"></td>
                                            <td>
                                                <button class="btn btn-primary btn-sm update_btn"
                                                        th:attr="update-id=${user.id}">
															<span class="glyphicon glyphicon-pencil"
                                                                  aria-hidden="true"></span> 编辑
                                                </button>
                                                <button class="btn btn-danger btn-sm delete_btn"
                                                        th:attr="delete-id=${user.id},delete-name=${user.username}">
															<span class="glyphicon glyphicon-trash"
                                                                  aria-hidden="true"></span> 删除
                                                </button>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            <!-- 显示分页信息 -->
                            <div class="row">

                                <!-- 新增按钮 -->
                                <div class="col-md-1" style="margin-left: 24px">
                                    <button id="user_add_modal_btn" class="btn btn-primary">新增</button>
                                </div>

                                <div class="col-md-5">
                                    <div class="btn-group">
                                        <button class="btn btn-default">
                                            <span>页次：</span>
                                        </button>
                                        <button class="btn btn-default">
                                            <span th:text="${userPageInfo.pageNum}+'/'+${userPageInfo.pages}+'页'"></span>
                                        </button>
                                        <button class="btn btn-default">
                                            <span>每页显示：</span>
                                        </button>
                                        <div class="btn-group">
                                            <button id="btnGroupDrop1"
                                                    class="btn btn-default dropdown-toggle"
                                                    data-toggle="dropdown" aria-haspopup="true"
                                                    aria-expanded="false"><span
                                                    th:text="${userPageInfo.pageSize}"></span>
                                                <span class="caret"></span>
                                            </button>
                                            <ul class="dropdown-menu">
                                                <li th:each="i:${#numbers.sequence(1,10)}">
                                                    <a th:text="${i}"
                                                       th:href="@{/admin/userinfo/toUserInfo.html(pageNum=${userPageInfo.pageNum},pageSize=${i})}"></a>
                                                </li>
                                            </ul>
                                        </div>
                                        <button class="btn btn-default">
                                            <span>条</span>
                                        </button>
                                        <button class="btn btn-default">
                                            <span>总记录数：</span>
                                        </button>
                                        <button class="btn btn-default">
                                            <span th:text="${userPageInfo.total}+'条'"></span>
                                        </button>
                                    </div>
                                </div>

                                <!-- 分页条信息 -->
                                <div class="col-md-5">
                                    <nav aria-label="Page navigation">
                                        <ul class="pagination pagination-lg pull-right"
                                            style="margin-top: 0">
                                            <li>
                                                <a th:href="@{/admin/userinfo/toUserInfo.html(pageNum=1,pageSize=${userPageInfo.pageSize})}">首页</a>
                                            </li>

                                            <li th:if="${userPageInfo.hasPreviousPage }"><a
                                                    th:href="@{/admin/userinfo/toUserInfo.html(pageNum=${userPageInfo.pageNum}-1,pageSize=${userPageInfo.pageSize})}"
                                                    aria-label="Previous"> <span aria-hidden="true">&laquo;</span>
                                            </a></li>

                                            <li th:each="page_Num:${userPageInfo.navigatepageNums }"
                                                th:class="${page_Num == userPageInfo.pageNum?'active':''}">
                                                <a th:if="${page_Num == userPageInfo.pageNum}"
                                                   th:text="${page_Num}"
                                                   th:href="@{#}">
                                                </a>
                                                <a th:text="${page_Num}"
                                                   th:if="${page_Num != userPageInfo.pageNum}"
                                                   th:href="@{/admin/userinfo/toUserInfo.html(pageNum=${page_Num},pageSize=${userPageInfo.pageSize})}">
                                                </a>
                                            </li>

                                            <li th:if="${userPageInfo.hasNextPage }"><a
                                                    th:href="@{/admin/userinfo/toUserInfo.html(pageNum=${userPageInfo.pageNum}+1,pageSize=${userPageInfo.pageSize})}"
                                                    aria-label="Next"> <span aria-hidden="true">&raquo;</span>
                                            </a></li>

                                            <li><a
                                                    th:href="@{/admin/userinfo/toUserInfo.html(pageNum=${userPageInfo.pages},pageSize=${userPageInfo.pageSize})}">末页</a>
                                            </li>
                                        </ul>
                                    </nav>
                                </div>
                            </div>

                        </div>
                    </div>

                    <!-- 新增用户的模态框 -->
                    <div class="modal fade" id="userAddModal" tabindex="-1"
                         role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                    <h4 class="modal-title">新增用户</h4>
                                </div>
                                <div class="modal-body">
                                    <!--要提交的表单 -->
                                    <form class="form-horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">用户名：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="username_add_input" name="username"
                                                       placeholder="请输入用户名：">
                                                <span class="help-block"></span>
                                            </div>
                                            <div class="col-sm-1">
                                                <em style="color: red">*</em>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">密码：</label>
                                            <div class="col-sm-6">
                                                <input type="password" class="form-control"
                                                       id="password_add_input" name="password"
                                                       placeholder="请输入密码：">
                                                <span class="help-block"></span>
                                            </div>
                                            <div class="col-sm-1">
                                                <em style="color: red">*</em>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">真实姓名：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="realname_add_input" name="realname"
                                                       placeholder="请输入真实姓名：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">手机号：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="phone_add_input" name="phone"
                                                       placeholder="请输入手机号：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">邮箱：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="email_add_input" name="email"
                                                       placeholder="请输入邮箱：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default"
                                            data-dismiss="modal">关闭
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            id="user_save_btn">保存
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 修改用户的模态框 -->
                    <div class="modal fade" id="userUpdateModal" tabindex="-1"
                         role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                    <h4 class="modal-title">修改用户信息</h4>
                                </div>
                                <div class="modal-body">
                                    <!--要提交的表单 -->
                                    <form class="form-horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">用户名：</label>
                                            <div class="col-sm-6">
                                                <p class="form-control-static"
                                                   id="username_update_input">
                                                </p>
                                                <span class="help-block"></span>
                                            </div>
                                            <div class="col-sm-1">
                                                <em style="color: red">*</em>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">真实姓名：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="realname_update_input" name="realname"
                                                       placeholder="该用户还未填写真实姓名：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">手机号：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="phone_update_input" name="phone"
                                                       placeholder="该用户还未填写手机号：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">邮箱：</label>
                                            <div class="col-sm-6">
                                                <input type="text" class="form-control"
                                                       id="email_update_input" name="email"
                                                       placeholder="该用户还未填写邮箱：">
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default"
                                            data-dismiss="modal">关闭
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            id="user_update_btn">更新
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<!-- 引入jQuery和bootstrap的js -->
<script th:src="@{/js/jquery.js}"></script>
<script th:src="@{/bootstrap/js/bootstrap.js}"></script>
<!--滚动条插件-->
<script th:src="@{/lyear/js/perfect-scrollbar.min.js}"></script>
<!--弹出框js-->
<script th:src="@{/lyear/jconfirm/jquery-confirm.min.js}"></script>
<!--自定义js脚本-->
<script th:src="@{/lyear/js/main.min.js}"></script>
<script th:src="@{/js/md5.js}"></script>
<script type="text/javascript">

    //点击新增按钮弹出模态框。
    $("#user_add_modal_btn").click(function () {
        //清除表单数据（表单完整重置（表单的数据，表单的样式））
        reset_form("#userAddModal form");
        //弹出模态框
        $("#userAddModal").modal({
            //点击增加框背景不删除增加框
            backdrop: "static"
        });
    });

    //点击保存，保存新增用户信息。
    $("#user_save_btn").click(function () {
        //清空表单样式
        clean_form("#userAddModal form");

        var username = $("#username_add_input").val();
        var password = $("#password_add_input").val();
        var realname = $("#realname_add_input").val();
        var phone = $("#phone_add_input").val();
        var email = $("#email_add_input").val();

        //匹配中文、英文、数字包括下划线(用户名)
        var usernameRegex = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
        if (username.length === 0) {
            show_validate_msg("#username_add_input",
                "error", "用户名为空");
            return false;
        } else if (usernameRegex.test(username) === false) {
            show_validate_msg("#username_add_input",
                "error", "用户名不合法（用户名只能由中文、数字、英文和下划线组成）");
            return false;
        }

        //匹配6-40位由数字、英文字母或者下划线组成的字符串(密码)
        var passwordRegex = /^\w{6,40}$/;
        if (password.length === 0) {
            show_validate_msg("#password_add_input",
                "error", "密码为空");
            return false;
        } else if (passwordRegex.test(password) === false) {
            show_validate_msg("#password_add_input",
                "error", "输入密码不合法（密码只能由数字、英文和下划线组成）");
            return false;
        }

        //匹配汉字(真实姓名)
        var realnameRegex = /^[\u4e00-\u9fa5]{2,10}$/;
        if (realname.length !== 0) {
            if (realnameRegex.test(realname) === false) {
                show_validate_msg("#realname_add_input",
                    "error", "真实姓名不合法");
                return false;
            }
        }

        //匹配手机号码
        var phoneRegex = /^1[3,5,8]\d{9}$/;
        if (phone.length !== 0) {
            if (phoneRegex.test(phone) === false) {
                show_validate_msg("#phone_add_input",
                    "error", "手机号不合法");
                return false;
            }
        }

        //匹配邮箱
        var emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (email.length !== 0) {
            if (emailRegex.test(email) === false) {
                show_validate_msg("#email_add_input",
                    "error", "邮箱不合法");
                return false;
            }
        }
        var pwd = $("#password").val();
        $("#password").val($.md5(pwd));
        //2、发送ajax请求保存用户
        $.ajax({
            url: "/user/addUser",
            type: "POST",
            data: $("#userAddModal form").serialize(),
            success: function (result) {
                if (result.code === 100) {

                    $("#userAddModal").modal('hide');
                    $.alert('用户新增成功！');
                    setTimeout(function () {
                        var pageNum = [[${userPageInfo.pages}]];
                        var pageSize = [[${userPageInfo.pageSize}]];
                        location.href = "/admin/userinfo/toUserInfo.html?pageNum=" + pageNum + "&pageSize=" + pageSize;
                    }, 1000);
                } else {
                    $.alert('用户新增失败！');
                }
            }
        });
    });

    //点击编辑按钮时触发事件
    $(document).on("click", ".update_btn", function () {

        //根据用户id发送ajax请求，查出用户信息并显示
        getUserInfo($(this).attr("update-id"));

        //把用户的id传递给模态框的更新按钮
        $("#user_update_btn").attr("update-id", $(this).attr("update-id"));
        $("#userUpdateModal").modal({
            backdrop: "static"
        });
    });

    //根据id查出用户信息并显示
    function getUserInfo(id) {
        $.ajax({
            url: "/user/getUserById/" + id,
            type: "GET",
            success: function (result) {
                var user = result.extend.user;

                //显示数据之前先清除一下校验样式，如果有的话
                reset_form("#userUpdateModal form");

                $("#username_update_input").text(user.username);
                $("#realname_update_input").val(user.realname);
                $("#phone_update_input").val(user.phone);
                $("#email_update_input").val(user.email);
            }
        });
    }

    //点击更新，更新用户信息
    $("#user_update_btn").click(function () {
        //清空表单样式
        clean_form("#userUpdateModal form");

        var realname = $("#realname_update_input").val();
        var phone = $("#phone_update_input").val();
        var email = $("#email_update_input").val();

        //匹配汉字(真实姓名)
        var realnameRegex = /^[\u4e00-\u9fa5]{2,10}$/;
        if (realnameRegex.test(realname) === false) {
            show_validate_msg("#realname_update_input",
                "error", "真实姓名不合法");
            return false;

        }

        //匹配手机号码
        var phoneRegex = /^1[3,5,8]\d{9}$/;
        if (phoneRegex.test(phone) === false) {
            show_validate_msg("#phone_update_input",
                "error", "手机号不合法");
            return false;

        }

        //匹配邮箱
        var emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (emailRegex.test(email) === false) {
            show_validate_msg("#email_update_input",
                "error", "邮箱不合法");
            return false;
        }


        //2、发送ajax请求保存更新的员工数据
        $.ajax({
            url: "/user/updateUserProfile/" + $(this).attr("update-id"),
            type: "PUT",
            data: $("#userUpdateModal form").serialize(),
            success: function (result) {
                if (result.code === 100) {
                    $("#userUpdateModal").modal('hide');
                    $.alert('用户信息修改成功！');
                    setTimeout(function () {
                        var pageNum = [[${userPageInfo.pageNum}]];
                        var pageSize = [[${userPageInfo.pageSize}]];
                        location.href = "/admin/userinfo/toUserInfo.html?pageNum=" + pageNum + "&pageSize=" + pageSize;
                    }, 1000);
                } else {
                    $.alert('用户信息修改失败！');
                }
            }
        });
    });

    //单个删除
    $(document).on("click", ".delete_btn", function () {
        //1、弹出是否确认删除对话框
        var id = $(this).attr("delete-id");
        var username = $(this).attr("delete-name");
        $.alert({
            title: '提示：',
            content: "确定删除用户 <strong>" + username + "</strong> 吗？",
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: function () {
                        $.ajax({
                            url: "/user/deleteUserById/" + id,
                            type: "DELETE",
                            success: function (result) {
                                if (result.code === 100) {
                                    $.alert('删除该用户成功！');
                                    setTimeout(function () {
                                        var pageNum = [[${userPageInfo.pageNum}]];
                                        var pageSize = [[${userPageInfo.pageSize}]];
                                        location.href = "/admin/userinfo/toUserInfo.html?pageNum=" + pageNum + "&pageSize=" + pageSize;
                                    }, 1000);
                                } else {
                                    $.alert('删除该用户失败!');
                                }
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    action: function () {
                        $.alert('已取消!');
                    }
                }
            }
        });

    });

    //清空表单样式及内容
    function reset_form(ele) {
        $(ele)[0].reset();
        //清空表单样式
        $(ele).find("*").removeClass("has-error has-success");
        $(ele).find(".help-block").text("");
    }

    //清空表单样式
    function clean_form(ele) {
        //清空表单样式
        $(ele).find("*").removeClass("has-error has-success");
        $(ele).find(".help-block").text("");
    }

    //显示校验结果的提示信息
    function show_validate_msg(ele, status, msg) {
        //清除当前元素的校验状态
        $(ele).parent().removeClass("has-success has-error");
        $(ele).next("span").text("");
        if ("success" === status) {
            $(ele).parent().addClass("has-success");
            $(ele).next("span").text(msg);
        } else if ("error" === status) {
            $(ele).parent().addClass("has-error");
            $(ele).next("span").text(msg);
        }
    }

</script>
</body>
</html>
